<template>
  <el-container>
    <el-header>
      <h2>修改密码</h2>
    </el-header>
    <el-main>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="用户名">
          <el-input :value="userbase.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="工牌号">
          <el-input :value="userbase.number" disabled></el-input>
        </el-form-item>
        <el-form-item label="原密码">
          <el-input v-model="form.passwd_old" clearable show-password></el-input>
        </el-form-item>
        <el-form-item label="新密码">
          <el-input v-model="form.passwd_new" clearable show-password></el-input>
        </el-form-item>
        <el-form-item label="确认密码">
          <el-input v-model="form.passwd_again" clearable show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" @click="onSubmit">修改密码</el-button>
        </el-form-item>
      </el-form>
    </el-main>
  </el-container>
</template>

<script>
export default {
  computed: {
    userbase() {
      return this.$store.state.login.userbase;
    }
  },
  data() {
    return {
      form: {
        passwd_old: "",
        passwd_new: "",
        passwd_again: ""
      }
    };
  },
  methods: {
    onSubmit() {
      if (this.checkPasswd()) {
        this.changePasswd(this.form.passwd_new);
      }
    },
    checkPasswd() {
      if (this.form.passwd_old != this.userbase.passwd) {
        this.$notify.error({ title: "错误", message: "原密码输入错误" });
        return false;
      }
      if (this.userbase.passwd == this.form.passwd_new) {
        this.$notify.warning({
          title: "密码未修改",
          message: "原密码和新密码一致"
        });
        return false;
      }
      if (this.form.passwd_new.length < 6) {
        this.$notify.error({
          title: "错误",
          message: "密码长度不应少于6位"
        });
        return false;
      }
      if (this.form.passwd_new != this.form.passwd_again) {
        this.$notify.error({
          title: "错误",
          message: "新密码输入不一致，请重新输入后再试"
        });
        return false;
      }
      return true;
    },
    changePasswd(passwd) {
      this.axios
        .post("api/modify_security_data_passwd/", {
          user: this.userbase,
          passwd: passwd
        })
        .then(response => {
          console.log(response.data);
          if (response.data.status == 0) {
            this.$notify.success({ title: "完成", message: "密码已修改" });
            this.$store.commit("setPasswd", passwd);
            this.$router.back(-1);
          } else if (response.data.status == 1) {
            this.$notify.error({ title: "错误", message: "原密码输入错误" });
          } else if (response.data.status == 2) {
            this.$notify.error({
              title: "错误",
              message: "写入数据错误，密码可能未修改成功"
            });
          } else {
            this.$notify.error({
              title: "错误",
              message: "未知错误，密码可能未修改成功"
            });
          }
        })
        .catch();
    }
  }
};
</script>